﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using MicropleSoft.Common.Library;
using MicropleSoft.Controls;
using MicropleSoft.WmsApp.Library.ClientProxy;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.Base.Menu
{
    public class TreeListBinder
    {
        public static void BindingTreeListMenu(XTreeList treeList)
        {
            treeList.Columns.Clear();
            treeList.Nodes.Clear();
            var treeListColumn = new TreeListColumn();
            treeList.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {treeListColumn});
            treeListColumn.FieldName = "Caption";
            treeListColumn.Name = "treeListColumn";
            treeListColumn.Visible = true;
            treeListColumn.OptionsColumn.AllowEdit = false;
            treeListColumn.OptionsColumn.ReadOnly = true;
            treeListColumn.VisibleIndex = 0;
            //var list = ClientProxyFactory.GetServiceBaseDataClient().GetMenuLists(" and LANG='zh' and COUNTRY='cn'");
            var models = ClientProxyFactory.GetServiceFacadeClient().GetMenuItemModels();
            treeList.BeginUnboundLoad();
            foreach (var menuItemModel in models)
            {
                var node = treeList.AppendNode(new object[] { menuItemModel.Caption },null);
                node.Tag = menuItemModel;
                InitNode(node);
            }
            treeList.EndUnboundLoad();
            //treeList.ExpandAll();
        }

        private static void InitNode(TreeListNode treeListNode)
        {
            var menuItemModel = (MenuItemModel) treeListNode.Tag;
            if (menuItemModel.Childrens != null)
            {
                foreach (var model in menuItemModel.Childrens)
                {
                    var node = treeListNode.TreeList.AppendNode(new object[] { model.Caption }, treeListNode);
                    node.Tag = model;
                    InitNode(node);
                }
            }
        }

        public static void BindingTreeListStaticData(XTreeList treeList, List<ItemClass> list, List<ItemClassKind> itemClassKindList)
        {
            treeList.Columns.Clear();
            treeList.Nodes.Clear();
            var treeListColumn = new TreeListColumn();
            treeList.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { treeListColumn });
            treeListColumn.FieldName = "Caption";
            treeListColumn.Name = "treeListColumn";
            treeListColumn.Visible = true;
            treeListColumn.OptionsColumn.AllowEdit = false;
            treeListColumn.OptionsColumn.ReadOnly = true;
            treeListColumn.VisibleIndex = 0;
           
            treeList.BeginUnboundLoad();

            var rootNodeList = itemClassKindList.Where(item => item.ParentKindId == ConstFlags.ParentKindId4Root);

            foreach (var itemClassKind in rootNodeList)
            {
                var node = treeList.AppendNode(new object[] { itemClassKind.KindName }, null);
                node.Tag = itemClassKind;
                //treeList.SetNodeIndex(node, itemClassKind.KindId);
                InitNode2(node, itemClassKindList);
            }

            int currentIndex = 10000;
            foreach (var itemClass in list)
            {
                var node = treeList.AppendNode(new object[] {itemClass.ClassName},
                                               FindNodeByItemClassKindId(treeList,
                                                                         itemClass.KindId == null ? 1: (int) itemClass.KindId));
                node.Tag = itemClass;
                //treeList.SetNodeIndex(node,currentIndex++);
            }
            treeList.EndUnboundLoad();
            treeList.ExpandAll();
        }

        private static TreeListNode FindNodeByItemClassKindId(XTreeList xTreeList, int kindId)
        {
            foreach (TreeListNode node in xTreeList.Nodes)
            {
                if( node.Tag is ItemClassKind )
                {
                    var itemClassKind = (ItemClassKind) node.Tag;
                    if(itemClassKind.KindId == kindId)
                        return node;
                }
            }
            return null;
        }

// ReSharper disable ParameterTypeCanBeEnumerable.Local
        private static void InitNode2(TreeListNode treeListNode, List<ItemClassKind> itemClassKindList)
// ReSharper restore ParameterTypeCanBeEnumerable.Local
        {
            var itemClassKind = (ItemClassKind)treeListNode.Tag;
            var tempList = itemClassKindList.Where(item => item.ParentKindId == itemClassKind.KindId);
            if (tempList .Count( ) > 0)
            {
                foreach (var classKind in tempList)
                {
                    var node = treeListNode.TreeList.AppendNode(new object[] { classKind.KindName }, treeListNode);
                    node.Tag = classKind;
                    //treeListNode.TreeList.SetNodeIndex(node, itemClassKind.KindId);
                    InitNode2(node, itemClassKindList);
                }
            }
        }

        /// <summary>
        /// 加载树形结构
        /// </summary>
        /// <param name="xTreeList"></param>
        /// <param name="classId"></param>
        /// <param name="itemNodes"></param>
        public static  void LoadTreeList(XTreeList xTreeList, int classId,List<ItemNode> itemNodes)
        {
            xTreeList.Columns.Clear();
            xTreeList.Nodes.Clear();

            var treeListColumn = new TreeListColumn();
            xTreeList.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { treeListColumn });
            treeListColumn.FieldName = "Caption";
            treeListColumn.Name = "treeListColumn";
            treeListColumn.Visible = true;
            treeListColumn.OptionsColumn.AllowEdit = false;
            treeListColumn.OptionsColumn.ReadOnly = true;
            treeListColumn.VisibleIndex = 0;

            xTreeList.BeginUnboundLoad();

            var nodeRoot = xTreeList.AppendNode(new object[] { "全部数据：" }, null);
            nodeRoot.Tag = null;
            var rootNodeList = itemNodes.Where(item => item.NodeParent == ConstFlags.ParentKindId4Root);
            foreach (var itemClassKind in rootNodeList)
            {
                var node = xTreeList.AppendNode(new object[] { itemClassKind.NodeName }, nodeRoot);
                node.Tag = itemClassKind;
                //xTreeList.SetNodeIndex(node, itemClassKind.NodeId);
                InitNode3( node, itemNodes);
            }
            xTreeList.EndUnboundLoad();
            xTreeList.ExpandAll();
        }

        
// ReSharper disable ParameterTypeCanBeEnumerable.Local
        private static void InitNode3(TreeListNode treeListNode, List<ItemNode> itemNodes)
// ReSharper restore ParameterTypeCanBeEnumerable.Local
        {
            var itemNodeParent = (ItemNode)treeListNode.Tag;
            var tempList = itemNodes.Where(item => item.NodeParent == itemNodeParent.NodeId);
            if (tempList.Count() > 0)
            {
                foreach (var itemNode in tempList)
                {
                    var node = treeListNode.TreeList.AppendNode(new object[] { itemNode.NodeName }, treeListNode);
                    node.Tag = itemNode;
                    //treeListNode.TreeList.SetNodeIndex(node, itemNode.NodeId);
                    InitNode3(node, itemNodes);
                }
            }
        }
    }
}

